const dayjs = require("dayjs");
exports.list = async function (req, res) {
    let { pid } = req.query;
    if (!pid) {
        return res.send({
            code: 0,
            msg: "pid",
        });
    }
    const sql = `SELECT * from  material WHERE status=? AND pid=? order by id desc`;
    const materialRes = await global.query(sql, [1, pid])
    res.send({
        code: 200,
        msg: "success",
        list: materialRes,
    });
};
exports.mlist = async function (req, res) {
    let { pid, status, page = 1, size = 10 } = req.query;
    if (!page) { page = 1 }
    if (!size) { size = 10 }
    const offset = (page - 1) * size; // 计算偏移量
    const sqlObj = []
    if (pid) { // null 1 2 全部 普通 代理
        sqlObj.push("pid=" + pid)
    }
    if (status) { // null 1 2 全部 普通 代理
        sqlObj.push("status=" + status)
    }
    const sqlcount = `SELECT COUNT(*) AS total FROM material ${sqlObj.length > 0 ? "WHERE " + sqlObj.join(" AND ") : ""}`
    const [countRes] = await global.query(sqlcount)
    const sql = `SELECT * from  material ${sqlObj.length > 0 ? "WHERE " + sqlObj.join(" AND ") : ""} ORDER BY id DESC LIMIT ${offset}, ${size || 10}`;
    const materialRes = await global.query(sql)
    res.send({
        code: 200,
        msg: "success",
        data: { list: materialRes, page: page || 1, size: size || 10, total: countRes.total },
    });
};

exports.get = async function (req, res) {
    let { id } = req.query;
    if (!id) {
        return res.send({
            code: 0,
            msg: "id",
        });
    }
    const sql = `SELECT * from  material WHERE id = ?`;
    const materialRes = await global.query(sql, [id])
    res.send({
        code: 200,
        msg: "success",
        data: materialRes[0],
    });
};

exports.add = async function (req, res) {
    const create_time = dayjs().format("YYYY-MM-DD HH:mm:ss");
    const { id, name, title, pid, images } = req.body;
    const sql = `INSERT INTO material(name,title,pid,images,create_time) VALUES(?,?,?,?,?)`;
    const sqlRes = await global.query(sql, [name, title, pid, images, create_time])
    if (sqlRes.affectedRows) {
        res.send({
            code: 200,
            msg: "添加成功",
        });
    } else {
        res.send({
            code: 0,
            msg: "添加失败",
        });
    }
};
exports.update = async function (req, res) {
    const create_time = dayjs().format("YYYY-MM-DD HH:mm:ss");
    const { id, name, title, images, pid } = req.body;
    console.log(id, name, title, images, pid)
    const sql = `UPDATE material SET  name=? , title=? , images=? , pid=?, create_time=? WHERE id = ?`;
    const sqlRes = await global.query(sql, [name, title, images, pid, create_time, id])
    if (sqlRes.affectedRows) {
        res.send({
            code: 200,
            msg: "修改成功",
        });
    } else {
        res.send({
            code: 0,
            msg: "修改失败",
        });
    }
};
exports.delete = async function (req, res) {
    const create_time = dayjs().format("YYYY-MM-DD HH:mm:ss");
    const { id, status } = req.body;
    const sql = `UPDATE material SET status=? ,create_time=? WHERE id = ?`;
    const sqlRes = await global.query(sql, [status || 1, create_time, id])
    if (sqlRes.affectedRows) {
        res.send({
            code: 200,
            msg: "删除成功",
        });
    } else {
        res.send({
            code: 0,
            msg: "删除失败",
        });
    }
};